package com.example.crm01.service.impl.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.crm01.entity.Business;
import com.example.crm01.entity.system.Region;
import com.example.crm01.mapper.system.RegionMapper;
import com.example.crm01.service.RegionService;
import com.example.crm01.service.form.BaseIdForm;
import com.example.crm01.service.form.system.RegionAddForm;
import com.example.crm01.service.form.system.RegionListForm;
import com.example.crm01.utils.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author songliang
 * @Package com.example.crm01.service.impl.system
 * @date 2024/7/1 1:13
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {

    public final RegionMapper regionMapper;

    public RegionServiceImpl(RegionMapper regionMapper) {
        this.regionMapper = regionMapper;
    }


    @Override
    public Page<Region> regionList(RegionListForm form) {
        Page<Region> page = new Page<>(form.getPageNum(), form.getPageSize());

        LambdaQueryWrapper<Region> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(form.getName())) {
            queryWrapper.like(Region::getName, form.getName());
        }
        if (StringUtils.isNotEmpty(form.getUsername())) {
            queryWrapper.like(Region::getUsername, form.getUsername());
        }
        if (ObjectUtils.isNotEmpty(form.getUserPhone())) {
            queryWrapper.eq(Region::getUsername, form.getUsername());
        }

        queryWrapper.ne(Region::getDel_flag, 2);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Boolean addRegion(RegionAddForm form) {
        Region region = new Region();
        region.setName(form.getName());
        region.setUsername(form.getUsername());
        region.setUserPhone(form.getUserPhone());

        //-----
        region.setCreateBy("1");
        region.setCreateTime(LocalDateTime.now());
        region.setUpdateBy("1");
        region.setUpdateTime(LocalDateTime.now());
        region.setDel_flag(0);
        int insert = regionMapper.insert(region);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean updateRegion(RegionAddForm form) {
        if (ObjectUtils.isEmpty(form.getId())){
            throw new RuntimeException("修改区划信息，主键id不能为空");
        }

        Region region = baseMapper.selectById(form.getId());
        region.setName(form.getName());
        region.setUsername(form.getUsername());
        region.setUserPhone(form.getUserPhone());

        //-----
        region.setUpdateBy("1");
        region.setUpdateTime(LocalDateTime.now());
        region.setDel_flag(0);
        int insert = regionMapper.insert(region);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean deleteRegion(BaseIdForm form) {
        Region region = baseMapper.selectById(form.getId());
        if (ObjectUtils.isEmpty(region)){
            throw new RuntimeException("区划信息不存在删除失败");
        }
       region.setUpdateBy("1");
       region.setUpdateTime(LocalDateTime.now());
       region.setDel_flag(2);
        int update = baseMapper.updateById(region);
        if (update > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
